package gold.digger;

import javafx.util.Pair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1190 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String reverseParentheses(String s) {
            return getPartRecur(s, 0).getKey().toString();
        }

        /**
         * 返回从当前位置curPos开始的部分结果，直到遇到）或字符串末尾结束
         *
         * @param s
         * @param curPos
         * @return
         */
        public Pair<StringBuilder, Integer> getPartRecur(String s, int curPos) {
            StringBuilder curPartRes = new StringBuilder();
            int i = curPos;
            while (i < s.length()) {
                if (Character.isAlphabetic(s.charAt(i))) curPartRes.append(s.substring(i, i++ + 1));
                else if ('(' == s.charAt(i)) {
                    Pair<StringBuilder, Integer> nextPartRecurRes = getPartRecur(s, i + 1);
                    curPartRes.append(nextPartRecurRes.getKey().reverse());
                    i = nextPartRecurRes.getValue();
                } else {
                    return new Pair<>(curPartRes, i + 1);
                }
            }

            return new Pair<>(curPartRes, i);
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.reverseParentheses("a(bcdefghijkl(mno)p)q"));
    }

    public static void main(String[] args) throws Exception {
        LC1190 an = new LC1190();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
